package com.fieldchain.ex.fa100.utils.share;

import android.app.Activity;
import android.content.SharedPreferences;
import android.util.Base64;
import android.util.Log;

import com.fieldchain.ex.fa100.access.MyApplication;
import com.fieldchain.ex.fa100.utils.string.AESUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * Created by biyunlong on 2016/9/4.
 */
public class ShareUtils {
    private String TAG = "ShareUtils";
    private static SharedPreferences mSharedPreferences;
    private static SharedPreferences.Editor editor;
    private static ShareUtils shareUtils;

    public static void init() {
        if (null == mSharedPreferences) {
            mSharedPreferences = MyApplication.getAppContext().getSharedPreferences("zy", Activity.MODE_PRIVATE);
        }
        if (null == editor) {
            editor = mSharedPreferences.edit();
        }

    }

    public static ShareUtils getInstance() {
        if (null == shareUtils) {
            shareUtils = new ShareUtils();
        }
        return shareUtils;
    }

    /**
     * 保存基本数据
     *
     * @param key
     * @param obj
     */
    public void saveObject(String key, Object obj) {
        if (obj instanceof Integer) {
            editor.putInt(key, (int) obj);
        } else if (obj instanceof Boolean) {
            editor.putBoolean(key, (boolean) obj);
        } else if (obj instanceof String) {
            editor.putString(key, (String) obj);
        } else if (obj instanceof Long) {
            editor.putLong(key, (Long) obj);
        } else if (obj instanceof Float) {
            editor.putFloat(key, (Float) obj);
        }
        editor.commit();
    }

    /**
     * 获取基本数据
     *
     * @param key
     */
    public int getInt(String key) {
        return mSharedPreferences.getInt(key, 0);
    }

    public String getString(String key) {
        return mSharedPreferences.getString(key, "");
    }

    public String getString(String key, String def) {
        return mSharedPreferences.getString(key, def);
    }

    public boolean getBoolean(String key) {
        return mSharedPreferences.getBoolean(key, false);
    }

    public boolean getBoolean(String key, boolean boo) {
        return mSharedPreferences.getBoolean(key, boo);
    }

    public Float getFloat(String key) {
        return mSharedPreferences.getFloat(key, 0);
    }


    public long getLong(String key) {
        return mSharedPreferences.getLong(key, 0);
    }

    public long getLongErr(String key) {
        return mSharedPreferences.getLong(key, -1);
    }

    /**
     * 保存复杂数据
     *
     * @param key
     * @param value
     */
    public synchronized void saveData(String key, Object value) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(value);
            String temp = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
            temp=AESUtils.encrypt(key,temp);
            editor.putString(key, temp);
            editor.commit();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (baos != null)
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }

    /**
     * 获取复杂数据的方法
     *
     * @param key
     * @return
     */
    public Object getData(String key) {
        String temp = mSharedPreferences.getString(key, "");
        if (temp.length() > 0) {
            try {
                temp = AESUtils.decrypt(key, temp);
                ByteArrayInputStream bais = new ByteArrayInputStream(Base64.decode(temp.getBytes(), Base64.DEFAULT));
                ObjectInputStream ois = new ObjectInputStream(bais);
                return ois.readObject();
            } catch (IOException e) {
                Log.i(TAG, e.toString());
            } catch (ClassNotFoundException e1) {
                Log.i(TAG, e1.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public void clearAll() {
        mSharedPreferences.edit().clear().commit();
    }

    public void clear(String key) {
        mSharedPreferences.edit().remove(key).commit();
    }

}
